<?php

class Users extends HTech_Db_Table
{
    protected $_name    = 'view_users';
    protected $_primary = 'UserId';
        
    protected $_lnkTables = array('LnkUsersLocations' => 'LocationId','LnkUsersPrograms' => 'ProgramId', 'LnkUsersOfficers' => 'OfficerId');
    protected $_dependentTables = array('LnkUsersLocations','LnkUsersPrograms','LnkUsersOfficers');    
    protected $_referenceMap    = array(
        'Roles' => array(
            'columns'           => 'RoleId',
            'refTableClass'     => 'Roles'
        )        
    );    

    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Users");
        $this->_gridMetadata['onClick'] = 'admin.user.add';
        $this->_gridMetadata['headers'] = array
            (
                array('Username',            70, $this->translate("Username"),          'String'),
                array('LocationShortName',   70, $this->translate("Office"),            'String'),
                array('Role',               150, $this->translate("Role"),              'String'),
                array('SystemAccessToDate', 150, $this->translate("Expiration"),        'DateTime', 'Date'),
                array('GovCode',            180, $this->translate("Gov. code"),         'String'),
                array('FullName',           250, $this->translate("Name"),              'String'),
                array('Email',              250, $this->translate("Email"),             'String'),
                array('MobilePhone',        100, $this->translate("Cell phone"),        'String'),
                array('LocationPhone',      100, $this->translate("Office phone"),      'String'),
                array('Status',             100, $this->translate("Status"),            'String'),
                array('Birthdate',          150, $this->translate("Birthdate"),    'DateTime', 'Date')
            );
        return $this;
    }

    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('locations' => null, 'programs' => null, 'officers' => null);
        $this->_record->r->locations = $this->_record->d->findManyToManyRowset('Locations','LnkUsersLocations');
        array_walk($this->_record->r->locations->toArray(), array($this, '_setRecordLnkData'), array('LnkUsersLocations', 'LocationId'));   
        $this->_record->r->programs = $this->_record->d->findManyToManyRowset('Programs','LnkUsersPrograms');
        array_walk($this->_record->r->programs->toArray(), array($this, '_setRecordLnkData'), array('LnkUsersPrograms', 'ProgramId'));   
        $this->_record->r->officers = $this->_record->d->findManyToManyRowset('Officers','LnkUsersOfficers');
        array_walk($this->_record->r->officers->toArray(), array($this, '_setRecordLnkData'), array('LnkUsersOfficers', 'OfficerId'));       
    }    
    
}
